<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2018/10/23

 * Time: 11:48

 */



namespace app\index\controller;

use page\Page;

use think\Db;



class Gift

{

    /**

     * 礼包列表

     */

    public function index()

    {



        $where['start_time'] = ['<=', date('Y-m-d H:i:s', time())];

        $where['end_time'] = ['>=', date('Y-m-d H:i:s', time())];

        $where['status'] = ['eq', 1];

        $where['is_delete'] = ['eq', 1];

        $where['gc.day'] = ['gt',0];

        $where['sycs'] = ['gt',0];

        $where['card_type'] = ['eq',1];



        $data = Db::name('gift g')

            ->fetchSql(false)

            ->where($where)

            ->join('gift_count gc', 'g.id = gc.gift_id')

            ->where('auding_status', 2)
			//->where('id','eq', 258)
			->where('g.id', 'neq', '258')
            ->field('g.id,g.sycs,title,sale_price,origin_price,single_pic,DATE_FORMAT(start_time,"%Y.%m.%d") start_time,DATE_FORMAT(end_time,"%Y.%m.%d") end_time,goods_sale sale_number,count')

            ->group('g.id')

            ->order(['sort' => 'asc','id' => 'desc'])

            ->paginate(1000)

            ->toArray();

        if (!empty($data)) {

            foreach ($data['data'] as $k => &$v) {

                $gift_count = Db::name('gift_count')->where('gift_id', $v['id'])->sum('gift_count');

                $v['gift_count'] = is_null($gift_count) ? 0 : $gift_count;

                $v['t_type'] = 1;

                $gift_service_count = Db::name('gift_count gc')

                    ->join('single s', 'gc.single_id = s.id', 'left')

                    ->where('gift_id', $v['id'])

                    ->field('cat_name,gift_count')

                    ->select();

                $gift_service_count_str = '';

                if (!empty($gift_service_count)) {

                        foreach ($gift_service_count as $k1 => $v1) {

                        $gift_service_count_str .= $v1['cat_name'] . ':' . $v1['gift_count'] . '次 ';

                    }

                }

                $v['gift_service_count'] = $gift_service_count_str;

            }

            $member_card = Db::name('system_card')

                ->alias('sc')

                ->join('system_card_count scc','sc.id = scc.gift_id','left')

                ->where('sc.day > 0 and sc.sale_price > 0 and sc.auding_status = 2 and sc.is_delete = 1')

                ->where('sc.status',1)

                ->field('sum(scc.gift_count) gc_sum,sc.id,sc.sycs,sc.title,sc.sale_price,sc.origin_price,sc.single_pic,sc.count,sc.gift_service_count,sc.type,sc.day,sc.sale_number')

                ->order(['sort' => 'asc','id' => 'desc'])

                ->having('gc_sum > 0')

                ->group('sc.id')

                ->select()->toArray();

            if (!empty($member_card)) {

                foreach ($member_card as $k1 => &$v1) {

                    //$v1['sale_price'] = 0.01;

                    //$v1['origin_price'] = mt_rand(1, 1000);

                    $v1['sycs'] = $v1['sycs'];

                    $v1['sale_number'] = $v1['sale_number'];

                    $v1['t_type'] = 2;

                    $v1['start_time'] = date('Y.m.d',time());

                    $v1['end_time'] = date('Y.m.d',time()+$v1['day']*3600*24);

                    if ($v1['type'] == 1) {

                        $v1['title'] = '(年卡)' . $v1['title'];

                        //$v1['single_pic'] = '/uploads/20190114/bef34313e30efae6c9da0eebfdbadc25.jpg';

                    }

                    if ($v1['type'] == 2) {

                        $v1['title'] = '(次卡)' . $v1['title'];

                        //$v1['single_pic'] = '/uploads/20190114/46a645f2e7236ff6a70ff92507f92603.png';

                    }

                }

            }



            if (is_null($member_card)) $member_card = [];

            $data = array_merge($data['data'], $member_card);

            $page = new Page(count($data), 100, []);

            $page_html = $page->show($data);// 分页显示输 __FUNCTION





        }



        return ['code' => 200, 'message' => '礼包列表', 'data' => $page_html];

    }



    public function detail($id,$member_id = 0,$t_type = 2){



        if($t_type == 2){



            return $this->detail_m($id,$member_id,$t_type);

        }

        $data = Db::name('gift')

            ->field('sycs,id,title,describe,sale_price,origin_price,single_pic,multi_pic,DATE_FORMAT(start_time,"%Y.%m.%d") start_time,DATE_FORMAT(end_time,"%Y.%m.%d") end_time,goods_sale sale_number,count')

            ->find($id);



        $data['multi_pic'] = unserialize($data['multi_pic']);

        $data1 = Db::name('config')->where('varname','in','web_service_tel,web_report_tel')->column('varvalue');



        if(!empty($member_id)){

            $auding_status['single_id'] = ['eq', $id];

            $auding_status['oe.member_id'] = ['eq', $member_id];

            $auding_status['order_type'] = ['eq', 3];

            $auding_status['t_type'] = ['eq', $t_type];

            $others_evaluate = function ($query) use ($id,$t_type) {

                $query->where('auding_status', 'eq', '2')->where('single_id', 'eq', $id)->where('order_type',3)->where('t_type',$t_type);

            };

            $evaluate_count = Db::name('order_evaluate oe')

                ->where($auding_status)->whereOr($others_evaluate)

                ->count('id');

            $evaluate_list = Db::name('order_evaluate oe')

                ->join('member m','oe.member_id = m.id','left')

                ->where($auding_status)->whereOr($others_evaluate)

                ->where('order_type',3)

                ->field('oe.*,m.username,m.nickname,m.avatar head_pic')

                ->order('oe.id','desc')

                ->paginate(6)->toArray();

        }else{

            $evaluate_count = Db::name('order_evaluate oe')

                ->where('order_type',3)

                ->where('t_type',$t_type)

                ->where(['auding_status' => 2,'single_id' => $id])

                ->count('id');

            $evaluate_list = Db::name('order_evaluate oe')

                ->order('oe.id','desc')

                ->join('member m','oe.member_id = m.id','left')

                ->where(['auding_status' => 2,'single_id' => $id])

                ->where('order_type',3)

                ->where('t_type',$t_type)

                ->field('oe.*,m.username,m.nickname,m.avatar head_pic')

                ->paginate(6)->toArray();

        }



        foreach ($evaluate_list['data'] as $k => &$v){

            $v['username'] = hideStr($v['username'],3,4);

        }



        return ['code' => 200,'message' => '礼包详情','data' => $data,'phone1' => $data1[0],'phone2' => $data1[0],'evaluate_count' => $evaluate_count,'evaluate_list' => $evaluate_list];

    }



    public function detail_m($id,$member_id = 0,$t_type){

        $data = Db::name('system_card')

            ->field('day,sycs,content,id,title,sale_price,origin_price,single_pic,DATE_FORMAT(start_time,"%Y.%m.%d") start_time,DATE_FORMAT(end_time,"%Y.%m.%d") end_time,sale_number,count')

            ->find($id);

        $data['start_time'] = date('Y.m.d',time());

        $data['end_time'] = date('Y.m.d',time()+$data['day']*3600*24);

        $data['multi_pic'] = [$data['single_pic']];

        $data['describe'] = '';

        $data1 = Db::name('config')->where('varname','in','web_service_tel,web_report_tel')->column('varvalue');



        if(!empty($member_id)){



            $auding_status['single_id'] = ['eq', $id];

            $auding_status['oe.member_id'] = ['eq', $member_id];

            $others_evaluate = function ($query) use ($id) {

                $query->where('auding_status', 'eq', '2')->where('single_id', 'eq', $id);

            };

            $evaluate_count = Db::name('order_evaluate oe')

                ->where($auding_status)->whereOr($others_evaluate)

                ->count('id');

            $evaluate_list = Db::name('order_evaluate oe')

                ->join('member m','oe.member_id = m.id','left')

                ->where($auding_status)->whereOr($others_evaluate)

                ->where('order_type',3)

                ->where('t_type',2)

                ->field('oe.*,m.username,m.nickname,m.avatar head_pic')

                ->order('oe.id','desc')

                ->paginate(6)->toArray();

        }else{



            $evaluate_count = Db::name('order_evaluate oe')

                ->where(['auding_status' => 2,'single_id' => $id])

                ->count('id');

            $evaluate_list = Db::name('order_evaluate oe')

                ->order('oe.id','desc')

                ->join('member m','oe.member_id = m.id','left')

                ->where(['auding_status' => 2,'single_id' => $id])

                ->where('t_type',2)

                ->where('order_type',3)

                ->field('oe.*,m.username,m.nickname,m.avatar head_pic')

                ->paginate(6)->toArray();

        }



        foreach ($evaluate_list['data'] as $k => &$v){

            $v['username'] = hideStr($v['username'],3,4);

        }

        return ['code' => 200,'message' => '礼包详情','data' => $data,'phone1' => $data1[0],'phone2' => $data1[0],'evaluate_count' => $evaluate_count,'evaluate_list' => $evaluate_list];

    }



    public function view($id,$field)

    {



        $data = Db::name('gift')->where('id',$id)->value($field);

        $content  = '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';

        $content .= '<style> img {vertical-align:middle; width:auto; max-width:100%;} </style>';

        $content .= $data;

        return response($content);

    }



    /**

     * @param $id

     * @param $field

     * @return \think\Response

     * 会员卡Web页面

     */

    public function member_card_view($id,$field)

    {

        $data = Db::name('system_card')->where('id',$id)->value($field);

        $content  = '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';

        $content .= '<style> img {vertical-align:middle; width:auto; max-width:100%;} </style>';

        $content .= $data;

        return response($content);

    }



    /**

     * @param $id

     * @param int $member_id

     * @param $order_type

     * @return array服务评价和礼包评价

     */



    public function evaluate_index($id, $member_id = 0,$order_type)

    {

        $auding_status['auding_status'] = ['eq', 2];

        $others_evaluate = [];

        $w = '';

        if (!empty($member_id)) {

            $auding_status['single_id'] = ['eq', $id];

            $auding_status['oe.member_id'] = ['eq', $member_id];

            $others_evaluate = function ($query) use ($id) {

                $query->where('auding_status', 'eq', '2')->where('single_id', 'eq', $id);

            };

            $evaluate_list = Db::name('order_evaluate oe')

                ->join('member m', 'oe.member_id = m.id')

                ->where('single_id', $id)

                ->where('order_type', $order_type)

                ->field('oe.*,m.username,m.nickname,m.avatar head_pic')

                ->where($auding_status)

                ->whereOr($others_evaluate)

                ->paginate(6)->toArray();



            foreach ($evaluate_list['data'] as $k => &$v) {

                $v['username'] = hideStr($v['username'], 3, 4);

            }

            $evaluate_count = Db::name('order_evaluate oe')->where('single_id', $id)->where('order_type', $order_type)->where($auding_status)

                ->whereOr($others_evaluate)->count('id');

        }else{



            $evaluate_list = Db::name('order_evaluate oe')

                ->join('member m', 'oe.member_id = m.id')

                ->where('single_id', $id)

                ->where('order_type', $order_type)

                ->field('oe.*,m.username,m.nickname,m.avatar head_pic')

                ->where($auding_status)

                ->whereOr($w)

                ->paginate(6)->toArray();



            foreach ($evaluate_list['data'] as $k => &$v) {

                $v['username'] = hideStr($v['username'], 3, 4);

            }

            $evaluate_count = Db::name('order_evaluate')->where('single_id', $id)->where('order_type', $order_type)->where($auding_status)

                ->whereOr($w)->count('id');

        }



        $order_name = '未知';

        if($order_type == 2){

            $order_name = '服务';

        }

        if($order_type == 3){

            $order_name = '礼包';

        }

        return ['code' => 200, 'message' => $order_name.'评价列表', 'evaluate_count' => $evaluate_count, 'evaluate_list' => $evaluate_list];

    }





}